package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.OrderaftersaleService;
import com.youi.entity.Orderaftersale;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 订单售后申请信息控制层
* @author Administrator
*/
@Controller
public class OrderaftersaleController  extends HttpRequestManage{

		@Autowired
		private OrderaftersaleService orderaftersaleService;

		/**
		 *添加或编辑订单售后申请信息
		*/
		@ResponseBody
		@RequestMapping("addOrEditOrderaftersale")
		public String addOrEditOrderaftersale(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Orderaftersale orderaftersale=null;
			if (id!=null) {
				orderaftersale=orderaftersaleService.get(id);
			}else {
				orderaftersale=new Orderaftersale();
				orderaftersale.setDelStatus(DelStatus.NODEL);
				orderaftersale.setGmtCreated(new Date());
			}
			Long userId=optLong("userId",request);
			if (userId!=null){
				orderaftersale.setUserId(userId);
			}
			Long orderId=optLong("orderId",request);
			if (orderId!=null){
				orderaftersale.setOrderId(orderId);
			}
			Integer status=optInteger("status",request);
			if (status!=null){
				orderaftersale.setStatus(status);
			}
			Integer isTake=optInteger("isTake",request);
			if (isTake!=null){
				orderaftersale.setIsTake(isTake);
			}
			String reason=optString("reason",request);
			if (reason!=null){
				orderaftersale.setReason(reason);
			}
			String imgs=optString("imgs",request);
			if (imgs!=null){
				orderaftersale.setImgs(imgs);
			}
			orderaftersale.setGmtModify(new Date());
			orderaftersaleService.saveOrUpdate(orderaftersale);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除订单售后申请信息
		*/
		@ResponseBody
		@RequestMapping("delOrderaftersaleByIds")
		public String delOrderaftersaleByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Orderaftersale> orderaftersaleList= orderaftersaleService.getByParams(params);
			if (orderaftersaleList==null||orderaftersaleList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Orderaftersale orderaftersale : orderaftersaleList) {
				orderaftersale.setDelStatus(DelStatus.DEL);
			}
			orderaftersaleService.saveOrUpdateAll(orderaftersaleList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * 分页查询订单售后申请信息
		*/
		@ResponseBody
		@RequestMapping("getOrderaftersale")
		public String getOrderaftersale(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Orderaftersale> orderaftersaleList=orderaftersaleService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (orderaftersaleList==null||orderaftersaleList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=orderaftersaleService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, orderaftersaleList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询订单售后申请信息
		*/
		@ResponseBody
		@RequestMapping("getOrderaftersaleById")
		public String getOrderaftersaleById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Orderaftersale orderaftersale=orderaftersaleService.get(id);
			if (orderaftersale==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",orderaftersale);//jsonObject	json对象
		}

		/**
		 * 查询所有订单售后申请信息
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllOrderaftersale")
		public String getAllOrderaftersale(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Orderaftersale> orderaftersaleList= orderaftersaleService.getByParams(null);
			if (orderaftersaleList==null||orderaftersaleList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",orderaftersaleList);//jsonObjectList	json对象集合
		}
}
